package com.rybbaby.tss.platform.tags;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.rybbaby.tss.platform.initializer.SysCityInitializer;
import com.rybbaby.tss.platform.sysCitys.entity.SysCitys;
import com.rybbaby.tss.platform.sysFiles.service.ISysFilesService;
import freemarker.template.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import freemarker.core.Environment;

/**
 * @description 城市信息获取标签,同时实现jsp和freemarker模板
 * 
 * @author 谢进伟
 * 
 * @createDate 2016年11月20日 下午9:37:59
 */
public class SysCitysTag extends SimpleTagSupport implements TemplateDirectiveModel {
	
	@Autowired
	private ISysFilesService sysFilesService;
	
	/*************************************** 针对freemarker视图的标签实现 ***************************************************/
	@Override
	public void execute(Environment env , Map params , TemplateModel [] loopVars , TemplateDirectiveBody body) throws TemplateException , IOException {
		try {
			String cityIds = "" , // 城市id字符串,多个id用第二个参数隔开
			type = "" , // 城市类型
			pid = "" , // 父id
			separator = ",";// 分隔符,默认为逗号
			Object paramValue = params.get("cityIds");
			if(paramValue instanceof TemplateScalarModel) {
				cityIds = ((TemplateScalarModel)paramValue).getAsString();
			}
			paramValue = params.get("separator");
			if(paramValue instanceof TemplateScalarModel) {
				separator = ((TemplateScalarModel)paramValue).getAsString();
			}
			paramValue = params.get("type");
			if(paramValue instanceof TemplateScalarModel) {
				type = ((TemplateScalarModel)paramValue).getAsString();
			}
			paramValue = params.get("pid");
			if(paramValue instanceof TemplateScalarModel) {
				pid = ((TemplateScalarModel)paramValue).getAsString();
			}
			separator = StringUtils.isBlank(separator) ? "," : separator;// 默认为逗号
			List<SysCitys> cityList = null;
			if(StringUtils.isNotBlank(pid)) {
				cityList = SysCityInitializer.getChildrenCity(pid);
			} else if(StringUtils.isNotBlank(type)) {
				cityList = SysCityInitializer.getCitysByLevelType(type);
			} else {
				cityList = getCityList(cityIds , separator);
			}
			if(body != null) {
				loopVars[0] = Configuration.getDefaultObjectWrapper(Configuration.getVersion()).wrap(cityList);
				body.render(env.getOut());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取城市列表
	 * 
	 * @param cityIdsStr
	 *            城市id字符串
	 * @param separator
	 *            分隔符
	 * @return
	 * @throws Exception
	 */
	private List<SysCitys> getCityList(String cityIdsStr , String separator) throws Exception {
		List<SysCitys> list = new ArrayList<SysCitys>();
		String [] split = cityIdsStr.split(separator);
		for(String cityId : split) {
			SysCitys city = SysCityInitializer.getCityById(cityId);
			if(city != null) {
				list.add(city);
			}
		}
		return list;
	}
	
	/*************************************** 针对jsp视图使的标签实现 ***************************************************/
	/** 城市id字符串 **/
	private String cityIds;
	/** 类型 **/
	private String type;
	/** 父Id **/
	private String pid;
	/** 多个城市Id之间的分隔符,默认:逗号(,) **/
	private String separator;
	/** 城市输出的变量名字 **/
	private String var;
	
	private void init() {
		if(StringUtils.isBlank(getCityIds())) {
			setCityIds("");
		}
		if(StringUtils.isBlank(getSeparator())) {
			setSeparator(",");
		}
		if(StringUtils.isBlank(type)) {
			setType("");
		}
		if(StringUtils.isBlank(type)) {
			setType("");
		}
	}
	
	@Override
	public void doTag() throws JspException , IOException {
		try {
			init();
			List<SysCitys> cityList = null;
			if(StringUtils.isNotBlank(pid)) {
				cityList = SysCityInitializer.getChildrenCity(pid);
			} else if(StringUtils.isNotBlank(type)) {
				cityList = SysCityInitializer.getCitysByLevelType(type);
			} else {
				cityList = getCityList(cityIds , separator);
			}
			if(cityList != null && !cityList.isEmpty()) {
				for(SysCitys sysFiles : cityList) {// 循环输出城市对象
					this.getJspContext().setAttribute(var , sysFiles , PageContext.PAGE_SCOPE);
					this.getJspBody().invoke(null);
				}
				this.getJspContext().removeAttribute(var);// 移除页面的临时变量,防止变量污染
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.doTag();
	}
	
	public String getCityIds() {
		return cityIds;
	}
	
	public void setCityIds(String cityId) {
		this.cityIds = cityId;
	}
	
	public String getSeparator() {
		return separator;
	}
	
	public void setSeparator(String separator) {
		this.separator = separator;
	}
	
	public String getVar() {
		return var;
	}
	
	public void setVar(String var) {
		this.var = var;
	}
	
	public String getType() {
		return type;
	}
	
	public void setType(String type) {
		this.type = type;
	}
	
	public String getPid() {
		return pid;
	}
	
	public void setPid(String pid) {
		this.pid = pid;
	}
	
}
